package helpers;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DateHelper {
	public static List<Long> calDistanceTimeToNow(Date date) {
		List<Long> arr = new ArrayList<Long>();
		Date now = new Date();
		long seconds = (now.getTime() - date.getTime())/1000;
		long day = (int) (seconds / 86400);
		long hour = (int) ((seconds - day*86400) / 3600);
		long minute = (int) ((seconds - day*86400 - hour*3600) / 60);
		arr.add(day);
		arr.add(hour);
		arr.add(minute);
		arr.add(seconds);
		return arr;
	}
	
	public static List<Long> coundownFromNow(Date date) {
		List<Long> arr = new ArrayList<Long>();
		Calendar cal=Calendar.getInstance();
		Date now = new Date();
		cal.setTime(date);
		cal.add(Calendar.DATE, +3);
		
		long seconds = (cal.getTime().getTime() - now.getTime())/1000;
		long day = (int) (seconds / 86400);
		long hour = (int) ((seconds - day*86400) / 3600);
		long minute = (int) ((seconds - day*86400 - hour*3600) / 60);
		
		arr.add(day);
		arr.add(hour);
		arr.add(minute);
		arr.add(seconds);
		return arr;
	}
	
	/**
	 * 
     * Example: System.out.println(DateHelper.isValidDate("2004-02-29"));
	 * 
	 */
	public static boolean isValidDate(String inDate) {

		if (inDate == null)
			return false;

		//set the format to use as a constructor argument
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		    
		if (inDate.trim().length() != dateFormat.toPattern().length())
		    return false;

		dateFormat.setLenient(false);
		    
		try {
		    //parse the inDate parameter
		    dateFormat.parse(inDate.trim());
		}
		catch (ParseException pe) {
		    return false;
		}
		return true;
	}

	/**
	 * @param inDate ex: 2012-02-29
	 * @param format ex: yyyy-MM-dd
	 * @return
	 */
	public static Date StringToDate(String inDate, String format) {
		Date date = new Date();
		if (inDate == null)
			return date;

		//set the format to use as a constructor argument
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		    
		/*if (inDate.trim().length() != dateFormat.toPattern().length())
		    return date;*/

		dateFormat.setLenient(false);
		    
		try {
		    //parse the inDate parameter
		    date = dateFormat.parse(inDate.trim());
		}
		catch (ParseException pe) {
		    return date;
		}
		return date;
	}
	
	
	public static Boolean checkGreaterNow(Date date, int minutes) {
		Boolean isGreater = false;
		Date now = new Date();
		if (now.getTime() - date.getTime() - minutes * 60000 > 0) {
			isGreater = false;
		} else {
			isGreater = true;
		}
		return isGreater;
	}
	
	public static Date setDate(String strDate, String formatDate) {
		Calendar cal = Calendar.getInstance();
		DateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy h:m:s");
		if (formatDate != null) {
			dateformat = new SimpleDateFormat(formatDate);
		}
		try {
			cal.setTime(dateformat.parse(strDate));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cal.getTime();
	}
	
	public static Calendar setCalendar(String strDate, String formatDate) {
		Calendar cal = Calendar.getInstance();
		DateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy h:m:s");
		if (formatDate != null) {
			dateformat = new SimpleDateFormat(formatDate);
		}
		try {
			cal.setTime(dateformat.parse(strDate));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cal;
	}
	
	public static String getStringFromDate(Date date, String formatDate) {
		DateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy h:m:s", Locale.ENGLISH);
		if (formatDate != null) {
			dateformat = new SimpleDateFormat(formatDate, Locale.ENGLISH);
		}
		
		String strDatePrize = (String) dateformat.format(date.getTime());
		return strDatePrize;
	}
	
	public static Date removeTimeFromDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	public static int getCountMonthFromNow(int startMonth, int startYear) {
		int countMonth = 0;
		
		Calendar cal = Calendar.getInstance(); 
		int currentMonth = cal.get(Calendar.MONTH) + 1;
		int currentYear = cal.get(Calendar.YEAR);
		
		countMonth = ((currentYear - startYear) * 12) + (currentMonth - startMonth);
		
		if (countMonth < 0) countMonth = 0;
		
		return countMonth;
	}
	
	public static String getMonthName(int month) {
		String[] months = {"January", "February",
			  "March", "April", "May", "June", "July",
			  "August", "September", "October", "November",
			  "December"};
		String monthName = months[month];
		
		return monthName;
	}
	
public static String convertStringDate(String inDate, String formatInput, String formatOutput) {
		
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(formatInput);
			

		dateFormat.setLenient(false);

		try {
			// parse the inDate parameter
			date = dateFormat.parse(inDate.trim());
		} catch (ParseException pe) {
		}
		
		dateFormat = new SimpleDateFormat(formatOutput);
		
		inDate = dateFormat.format(date);
		
		return inDate;
	}
	
	public static String getEndMonth(String inDate, String formatInput) {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(formatInput);
			

		dateFormat.setLenient(false);

		try {
			// parse the inDate parameter
			date = dateFormat.parse(inDate.trim());
		} catch (ParseException pe) {
		}
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		
		int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		date.setDate(maxDay);
		
		inDate = dateFormat.format(date);
		
		return inDate;
	}
	
	public static void main(String[] args) {
		String inDate = "30-12-2012";
		String formatInput = "dd-MM-yyyy";
		String formatOutput = "yyyy-MM-dd";
		
		String date = convertStringDate(inDate, formatInput, formatOutput);
		
		System.out.println(getEndMonth(date, formatOutput));
		
	}
}
